const { regClass } = Laya;
import HttpAPI from "../../HttpManager/HttpAPI";
import HttpManager from "../../HttpManager/HttpManager";
import { ViewDetailBase } from "./ViewDetail.generated";

@regClass()
export class ViewDetail extends ViewDetailBase {
    private currenPage: number=1;
    private totalPage: number;
    private arrDetailList: Array<any> = [];
    onAwake(): void {
        this.init();
        this.getDetailInfo(this.currenPage);
        
    }
    init() {
        this.createDataList();
    }
    private getDetailInfo(num:number) {
        HttpManager.post(HttpAPI.POST_USER_INTRGRALDETAIL, {pageNo: num,pageSize:8}, null).then(this.onGetDetailList.bind(this)).catch(this.onGetDetailList.bind(this));
    }

    private onGetDetailList(data: any) {
        this.arrDetailList = [];
        if (data && data.success) {
            const result = data.result.records;
            this.currenPage=data.result.current;
            this.totalPage=data.result.pages;
            for (let item of result) {
                this.arrDetailList.push({ DetailName: item.name, Time: item.createTime, Score: item.integral,Type:item.type});
            }
            this.ChangePage.onChangePage(this.totalPage, this.currenPage);
        }
        if (this.arrDetailList.length == 0) {
            this.DetailList.visible = false;
            this.NoneState.visible = true;
        }
        else {
            this.NoneState.visible = false;
            this.DetailList.visible = true;
            this.updataRecordList();
        }
        this.addEvent();
    }
    updataRecordList() {
        this.DetailList.array = this.arrDetailList;
        this.DetailList.repeatY = this.arrDetailList.length / 2;
        if (this.arrDetailList.length < 2) {
            this.DetailList.repeatX = this.arrDetailList.length;
        }
        else {
            this.DetailList.repeatX = 2;
        }
        this.DetailList.refresh();

    }
    createDataList() {
        this.DetailList.renderHandler = Laya.Handler.create(this, this.onRenderDetailList, undefined, false);
        this.DetailList.array = this.arrDetailList;
    }
    onRenderDetailList(item: Laya.Box, index: number) {
        if (index > this.DetailList.array.length || index < 0) return;
        let detailname = item.getChildByName("DetailName") as Laya.Label;
        let time = item.getChildByName("Time") as Laya.Label;
        let score = item.getChildByName("Score") as Laya.Label;
        let detailbg=item.getChildByName("DetailBg") as Laya.Image;
        const data = this.DetailList.array[index];
        detailname.text = data.DetailName;
        time.text = data.Time;
        if(data.Type==2)
        {
            detailbg.skin="atlas/UI/Shop/box_jfmx_b@1x.png"
            score.text ="-"+ data.Score;
            score.color="#E7465C";
        }
        else
        {
            detailbg.skin="atlas/UI/Shop/box_jfmx_a@1x.png"
            score.text = "+"+data.Score;
            score.color="#146261"
        }
    }

    onRChangePage() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        if (this.currenPage >= this.totalPage) { return; }
        this.currenPage++;
        this.getDetailInfo(this.currenPage);
        this.ChangePage.onChangePage(this.totalPage, this.currenPage);

    }
    onLChangePage() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        if (this.currenPage <= 1) { return; }
        this.currenPage--;
        this.getDetailInfo(this.currenPage);
        this.ChangePage.onChangePage(this.totalPage, this.currenPage);
    }
    onClickChangePage(num: number, event: Laya.Event) {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        let obj = this.ChangePage.getChildByName("PageBox").getChildAt(num).getChildByName("BG").getChildByName("Label") as Laya.Label;
        this.currenPage = parseInt(obj.text);
        console.log(this.currenPage);
        
        this.getDetailInfo(this.currenPage);
        this.ChangePage.onChangePage(this.totalPage, this.currenPage);
    }
    addEvent() {
        this.removeEvent()
        this.BtnClose.on(Laya.Event.CLICK, this, this.onBtnClose)
        this.ChangePage.getChildByName("LPageBG").on(Laya.Event.CLICK, this, this.onLChangePage)
        this.ChangePage.getChildByName("RPageBG").on(Laya.Event.CLICK, this, this.onRChangePage)
        const pageBox = this.ChangePage.getChildByName("PageBox");
        for (let i = 0; i < pageBox.numChildren; i++) {
            const child = pageBox.getChildAt(i);
            let btn = child.getChildByName("BG").getChildByName("Label") as Laya.Label
            child.on(Laya.Event.CLICK, this, this.onClickChangePage, [i]);
        }
    }
    removeEvent() {
        this.BtnClose.off(Laya.Event.CLICK, this, this.onBtnClose)
        this.ChangePage.getChildByName("LPageBG").off(Laya.Event.CLICK, this, this.onLChangePage)
        this.ChangePage.getChildByName("RPageBG").off(Laya.Event.CLICK, this, this.onRChangePage)
        const pageBox = this.ChangePage.getChildByName("PageBox");
        for (let i = 0; i < pageBox.numChildren; i++) {
            const child = pageBox.getChildAt(i);
            let btn = child.getChildByName("BG").getChildByName("Label") as Laya.Label
            child.off(Laya.Event.CLICK, this, this.onClickChangePage, [i]);
        }
    }
    onBtnClose() {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.destroy();
    }
}